package interfaces.spawnMenu.squads;

import java.util.Iterator;

import interfaces.spawnMenu.AbsSpawnMenu;
import interfaces.spawnMenu.SpawnMenuWindow;
import logic.common.Squad;
import logic.common.player.Player;
import logic.common.team.Team;
import logic.ships.hunter.Hunter;

import org.fenggui.layout.StaticLayout;

import fileHandling.language.interfaces.SpawnMenuText;

public class SquadsWindow extends SpawnMenuWindow {
	
	public static final int MAX_SQUADS = 8;
	
	private Player player;
	private int squadWidth, squadHeight, lastX, lastY, contents;
	
	public SquadsWindow(Team team, Team enemyTeam, AbsSpawnMenu spawnMenu) {
		super(SpawnMenuText.Squads, team, enemyTeam, spawnMenu);
		
		player = spawnMenu.getGame().getCurrentPlayer();
		
		content.setLayoutManager(new StaticLayout());
		layout();
		
		squadWidth = content.getWidth() / 2;
		squadHeight = content.getHeight() / 4;
		initLastXLastY();
		
		initSquads();
		
		layout();
	}
	
	private void initLastXLastY() {
		contents = 0;
		lastX = squadWidth;
		lastY = content.getHeight() - squadHeight;
	}
	
	private void initSquads() {
		Iterator<Squad> iter = team.getAllSquadIterator();
		while(iter.hasNext()) {
			Squad squad = iter.next();
			SquadContent squadContent = getSquadContent(squad);
			content.addWidget(squadContent);
		}
	}
	
	private SquadContent getSquadContent(Squad squad) {
		int x = lastX == 0 ? squadWidth : 0;
		lastX = x;
		
		int y = lastY;
		if(contents != 0 && contents % 2 == 0) y -= squadHeight;
		lastY = y;
		contents++;
		return new SquadContent(player, squadWidth, squadHeight, x, y, spawnMenu, squad);
	}
	
	@Override
	public void changeTeam(Team newTeam) {
		super.changeTeam(newTeam);
		
		initLastXLastY();
		initSquads();
		
		layout();
	}
	
	@Override
	public void changeHunter(Hunter hunter) {}
}